package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CancelOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhangminghui
 * @date 2022/8/24
 */
@RestController
public class ZmhShoppingOrderController {

    @Autowired
    OrderQueryService orderQueryService;

    @Autowired
    OrderCoreService orderCoreService;

    @GetMapping("/shopping/order")
    //public ResponseData shoppingOrder(@RequestParam OrderListRequest orderListRequest) {
    public ResponseData shoppingOrder(@RequestParam Integer size, Integer page, HttpServletRequest request) {
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        long uid = Long.parseLong(object.get("uid").toString());
        OrderListRequest orderListRequest = new OrderListRequest();
        orderListRequest.setSize(size);
        orderListRequest.setPage(page);
        orderListRequest.setUserId(uid);
        OrderListResponse result = orderQueryService.orderList(orderListRequest);
        if (result != null) {
            String msg = "success";
            ResponseData responseData = new ResponseUtil().setData(result, msg);
            return responseData;
        } else {
            String msg = "fail";
            ResponseData responseData = new ResponseUtil().setErrorMsg(msg);
            return responseData;
        }
    }

    @GetMapping("/shopping/order/*")
    //public ResponseData orderDetail(@RequestParam String orderId) {
    public ResponseData orderDetail(HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        String orderId = requestURI.replace(request.getContextPath() + "/shopping/order/", "");
        OrderDetailResponse result = orderQueryService.orderDetail(orderId);

        if (result != null) {
            String msg = "success";
            ResponseData responseData = new ResponseUtil().setData(result, msg);
            return responseData;
        } else {
            String msg = "fail";
            ResponseData responseData = new ResponseUtil().setErrorMsg(msg);
            return responseData;
        }
    }

    //@GetMapping("/shopping/cancelOrder")
    @PostMapping("/shopping/cancelOrder") //todo:postmapping...
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest) {
        //public ResponseData cancelOrder(String orderId) {
        orderCoreService.cancelOrder(cancelOrderRequest.getOrderId());
        //orderCoreService.cancelOrder(orderId);
        Object result = null;
        String msg = null;
        ResponseData responseData = new ResponseUtil().setData(result, msg);
        return responseData;
    }

}
